package com.learn.cn.useMq;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/** @author admin 2019/12/12-9:39 */
public class JmsConsumer {
  /*默认连接用户名*/
  private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
  /* 默认连接密码*/
  private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;

  /* 默认连接地址*/
  private static final String BROKENURL = ActiveMQConnection.DEFAULT_BROKER_URL;

  public static void main(String[] args) {

    /* 连接工厂*/
    ConnectionFactory connectionFactory;
    /* 连接*/
    Connection connection = null;
    /* 会话*/
    Session session;
    /* 消息的目的地*/
    Destination destination;
    /* 消息的消费者*/
    MessageConsumer messageConsumer;
    /* 实例化连接工厂*/
    connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKENURL);

    try {
      /* 通过连接工厂获取连接*/
      connection = connectionFactory.createConnection();
      /* 启动连接*/
      connection.start();
      /* 创建session
       * 第一个参数表示是否使用事务，第二次参数表示是否自动确认*/
      session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      /* 创建一个名为HelloWorld消息队列*/
     destination = session.createQueue("HelloWorld");
     // destination = session.createTopic("HelloWorldTopic");
      /* 创建消息生产者*/
      messageConsumer = session.createConsumer(destination);

      Message message;
      while ((message = messageConsumer.receive()) != null) {
        String msg=((TextMessage) message).getText();
        System.out.println(msg);
      }
      /* 循环发送消息*/
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (connection != null) {
        try {
          connection.close();
        } catch (JMSException e) {
          e.printStackTrace();
        }
      }
    }
  }
}
